package controllers;

import java.util.Date;
import java.util.List;

import models.Business;
import models.BusinessIndicator;
import models.IndicatorCaliber;
import models.User;

import org.apache.commons.lang.StringUtils;

import play.data.validation.Required;
import utils.Paged;

public class IndicatorCaliberController extends BaseController{
    
    private static String createQueryHql(String name, Long businessId, Long authorId) {
        StringBuilder hql = new StringBuilder(" 1 = 1 ");
        if (!StringUtils.isBlank(name)) {
            hql.append(" and name like '%").append(name).append("%' ");
        }
        if (businessId != null) {
            hql.append(" and business.id = '").append(businessId).append("'");
        }
        if (authorId != null) {
            hql.append(" and author.id = '").append(authorId).append("'");
        }
        return hql.toString();
    }
    
    public static void list(String name, Long businessId, Long authorId, int currentPage) {
        String hql = createQueryHql(name, businessId, authorId);
        currentPage = getCurrentPage(currentPage);
        int count = new Long(IndicatorCaliber.count(hql)).intValue();
        List<IndicatorCaliber> tables = IndicatorCaliber.find(hql).from((currentPage - 1) * PAGE_SIZE).fetch(PAGE_SIZE);
        Paged<IndicatorCaliber> paged = new Paged<IndicatorCaliber>(tables, currentPage, PAGE_SIZE, count);
        List<User> authors =  User.findActiveUsers();
        List<Business> businesses = Business.findActiveBusinesses();
        render("/caliber/list.html", paged, name, businessId, authorId, authors, businesses);
    }
    
    public static void form(Long indicatorId) {
        List<User> authors = User.findActiveUsers();
        List<Business> businesses = Business.findActiveBusinesses();
        if (indicatorId == null) {
            render("/caliber/form.html", authors, businesses);
        }
        
        IndicatorCaliber caliber = IndicatorCaliber.findById(indicatorId);
        if (caliber == null) {
            notFound();
        }
        
        render("/caliber/form.html", caliber, authors, businesses);
    }
    
    public static IndicatorCaliber toSetCaliberValue(IndicatorCaliber caliber, Long businessId, Long authorId, String name, String description, String remark, String sqlStr) {
        caliber.author = new User(authorId);
        caliber.business = new Business(businessId);
        caliber.name = name;
        caliber.description = description;
        caliber.remark = remark;
        caliber.updateDate = new Date();
        caliber.sqlStr = sqlStr;
        return caliber;
    }
    
    
    
    public static void save(@Required(message = "caliber.business.require")Long businessId,
                            @Required(message = "caliber.author.require") Long authorId, 
                            @Required(message = "caliber.name.require") String name, 
                            String description, String remark, String sqlStr) {
        if (validation.hasErrors()) {
            params.flash();
            validation.keep();
            form(null);
        }
        
        IndicatorCaliber caliber = new IndicatorCaliber();
        caliber = toSetCaliberValue(caliber, businessId, authorId, name, description, remark, sqlStr).save();
        form(caliber.id);
    }
    
    public static void update(Long caliberId,
                            @Required(message = "caliber.business.require")Long businessId,
                            @Required(message = "caliber.author.require") Long authorId, 
                            @Required(message = "caliber.name.require") String name, 
                            String description, String remark, String sqlStr) {
        if (validation.hasErrors()) {
            params.flash();
            validation.keep();
            form(caliberId);
        }
        IndicatorCaliber caliber = IndicatorCaliber.findById(caliberId);
        if (caliber == null ) {
            notFound();
        }
        caliber = toSetCaliberValue(caliber, businessId, authorId, name, description, remark, sqlStr).save();
        form(caliber.id);
    }
    
    public static void delete (Long caliberId) {
        IndicatorCaliber.delete("id", caliberId);
        list(null, null, null, FIRST_PAGE);
    }

}
